package com.ay.wiki_plt.controller;

import com.ay.wiki_plt.common.PageBean;
import com.ay.wiki_plt.common.Result;
import com.ay.wiki_plt.domain.Category;
import com.ay.wiki_plt.domain.dto.PageRequestDTO;
import com.ay.wiki_plt.service.CategoryService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;

    /*查询全部分类*/
    @GetMapping("/list")
    public Result list(){
        log.info("查询全部分类");
        List<Category> categoryList = categoryService.getCategoryList();
        return Result.success(categoryList);
    }


    /*查询全部分类分页*/
    @GetMapping("/list/page")
    public Result list(
            @Valid PageRequestDTO pageRequestDTO,
            @RequestParam(defaultValue = "") String name) {
        log.info("查询电子书列表,模糊查询:{},{},{}", pageRequestDTO.getPage(), pageRequestDTO.getPageSize(), name);
        PageBean pageBean = categoryService.list(pageRequestDTO.getPage(), pageRequestDTO.getPageSize(), name);
        return Result.success(pageBean);
    }


    /*根据id查询分类*/
    @GetMapping("/{id}")
    public Result listOne(@PathVariable Integer id) {
        log.info("查询的id:{}", id);
        Category category = categoryService.listOne(id);
        return Result.success(category);
    }


    //分类保存接口
    @PostMapping
    public Result save(@RequestBody Category Category){
        log.info("保存分类:{}",Category);
        //调用service
        categoryService.save(Category);
        return Result.success();
    }

    //删除分类
    //参数传递 /1,2,3
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Long> ids){
        log.info("删除分类id:{}",ids);
        categoryService.delete(ids);
        return Result.success();
    }



}
